<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="add"> 新增 </el-button>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd" />
    <!--表格渲染-->
    <el-table
      :data="tableData"
      :tree-props="{children: 'children'}"
      row-key="id"
      size="small"
      class="table-fixed"
    >
      <el-table-column prop="name" :show-overflow-tooltip="true" label="名称" width="120px" />
      <el-table-column prop="enabled" label="状态" align="center" min-width="80px">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            @change="changeEnabled(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建日期" min-width="150px" />
      <el-table-column label="操作" min-width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)" />
          <el-popconfirm
            title="确定删除吗? 如果存在下级节点则一并删除，此操作不能撤销！"
            @onConfirm="subDelete(scope.row.id)"
          >
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" />
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getDepartments, removeDepartment, editDepartment } from '@/api/system/department'
import eForm from './form'
export default {
  name: 'Department',
  components: { eForm },
  data() {
    return {
      tableData: [],
      isAdd: false,
      delLoading: false
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    loadData() {
      getDepartments().then(res => {
        this.tableData = res.data
      })
    },
    changeEnabled(data) {
      this.$confirm('此操作将 "' + (data.enabled ? '启用' : '禁用') + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        editDepartment(data).then(res => {
          this.$notify({
            title: (data.enabled ? '启用' : '禁用') + '成功',
            type: 'success',
            duration: 2500
          })
        })
      }).catch(() => {
        data.enabled = !data.enabled
      })
    },
    add() {
      this.isAdd = true
      const _this = this.$refs.form
      _this.getDepartments()
      _this.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.getDepartments()
      if (data.parentId === '0') {
        _this.isTop = 'true'
      } else {
        _this.isTop = 'false'
      }
      _this.form = { ...data }
      _this.dialog = true
    },
    subDelete(id) {
      removeDepartment(id).then(res => {
        this.loadData()
        this.$notify({ title: '删除成功', type: 'success', duration: 2500 })
      })
    }
  }
}
</script>

<style scoped>
</style>
